package com.ewhale.feitengguest.ui.user.mine;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ewhale.feitengguest.R;
import com.ewhale.feitengguest.dto.MyTaskDto;
import com.ewhale.feitengguest.presenter.user.mine.MyTaskPresenter;
import com.ewhale.feitengguest.ui.user.mine.adapter.MyTaskAdapter;
import com.ewhale.feitengguest.view.user.mine.MyTaskView;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.simga.library.activity.MBaseFragment;
import com.simga.library.adapter.recyclerview.OnItemListener;
import com.simga.library.utils.Dp2PxUtil;
import com.simga.library.widget.CoustomRefreshView;
import com.simga.library.widget.RecycleViewDivider;
import com.youth.xframe.widget.loadingview.XLoadingView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MyTaskFragment extends MBaseFragment<MyTaskPresenter> implements MyTaskView {
    @BindView(R.id.list)
    RecyclerView mList;
    @BindView(R.id.ref_layout)
    CoustomRefreshView mRefLayout;
    @BindView(R.id.xloading_view)
    XLoadingView xloadingView;

    public List<MyTaskDto> datas;
    public MyTaskAdapter mMyTaskAdapter;
    private int pageNum = 1;

    @Override
    protected int getViewId() {
        return R.layout.fragment_base_list;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
    }

    @Override
    public void onResume() {
        super.onResume();
        pageNum = 1;
        xloadingView.showLoading();
        presenter.loadTaskList(pageNum, getArguments().getInt("type"));
    }

    @Override
    protected void firstInit(Bundle savedInstanceState) {
        datas = new ArrayList<>();
        mList.setLayoutManager(new LinearLayoutManager(mContext));
        mList.setHasFixedSize(true);
        mList.addItemDecoration(new RecycleViewDivider(mContext,
                Dp2PxUtil.dip2px(mContext, 10)));
        mMyTaskAdapter = new MyTaskAdapter(datas);
        mList.setAdapter(mMyTaskAdapter);
    }

    @Override
    protected void initListener() {
        mMyTaskAdapter.setOnItemClickListener(new OnItemListener() {
            @Override
            public void onItem(View view, int position) {
                MyTaskDetailActivity.open(mContext, datas.get(position).getId());
            }
        });
        mRefLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                super.onRefresh(refreshLayout);
                pageNum = 1;
                presenter.loadTaskList(pageNum, getArguments().getInt("type"));
            }

            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                super.onLoadMore(refreshLayout);
                pageNum++;
                presenter.loadTaskList(pageNum, getArguments().getInt("type"));
            }
        });
        xloadingView.setOnRetryClickListener(v -> {
            pageNum = 1;
            xloadingView.showLoading();
            presenter.loadTaskList(pageNum, getArguments().getInt("type"));
        });
    }

    @Override
    public void showProLoading() {
        showLoading();
    }

    @Override
    public void dimissProLoading() {
        dismissLoading();
    }

    @Override
    public void showErrorMessage(int postCode, String code, String message) {
        showErrorMsg(code, message);
        if(postCode == 1){
            xloadingView.showError();
        }
        onLoad(-1);
    }

    public static MyTaskFragment getInstance(int type) {
        MyTaskFragment fragment = new MyTaskFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fragment.setArguments(bundle);
        return fragment;
    }

    public void onLoad(int size) {
        if (mRefLayout == null) {
            return;
        }
        mRefLayout.finishRefreshLoadingMore();
        if (size < 10) {
            mRefLayout.enableLoadMore(false);
        } else {
            mRefLayout.enableLoadMore(true);
        }
    }

    @Override
    public void showTaskList(List<MyTaskDto> result, int pageNum) {
        if (pageNum == 1) {
            datas.clear();
        }
        datas.addAll(result);
        mMyTaskAdapter.notifyDataSetChanged();
        if(datas.size() == 0){
            xloadingView.showEmpty();
        }else{
            xloadingView.showContent();
        }
        onLoad(result.size());
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMyTaskAdapter.cancelAllTimers();
    }

    @Override
    public void onPause() {
        super.onPause();
        mMyTaskAdapter.cancelAllTimers();
    }
}
